package controller;
import model.Roles;
import java.util.ArrayList;
import java.sql.ResultSet;

public class RolesMgr extends DbManager {

	public static final RolesMgr mgr = new RolesMgr();

	public RolesMgr() {
		super( "Roles" );
		m_titles = new String[]{"idRol", "nombreRol", "nitEmpresa", "permisos"};
	}

	@Override
	protected Roles getBean() {
		return new Roles();
	}

	@Override
	protected void addObject( ArrayList v, ResultSet rs ) {
		v.add( new Roles( rs ) );
	}

	@Override
	public synchronized Roles getItem( String id ) {
		return (Roles)super.getItem(id);
	}

        public synchronized ArrayList<Roles> getListPorUsuario(String cedula) {

            String sqlRoles = "select r.* from Roles r, UsuariosRoles u where u.cedulaUsuario = '"+cedula+"' and r.idRol = u.rolUsuario";
            ArrayList<Roles> lst = executeQuery(sqlRoles);
            return lst;
        }

        public synchronized void crearRolAdmin(String idEmpresa){
            execute("insert into Roles (nombreRol, nitEmpresa, permisos) values ('Administrador Tecnico', '"+idEmpresa+"', 1)");
        }

        public synchronized Roles getRolSuperAdmin(String idEmpresa){
            ArrayList<Roles> lst = executeQuery("select * from Roles where permisos = 1 and nitEmpresa = '"+idEmpresa+"'");
            if (lst.size()>0){
                return lst.get(0);
            }else{
                return new Roles();
            }
        }

        public synchronized ArrayList<Roles> getListForEmpresa (String idEmpresa){
            ArrayList<Roles> lst = executeQuery("select * from Roles where nitEmpresa = '"+idEmpresa+"' and permisos <> 2 and permisos <> 1");
            return lst;
        }

        public synchronized void eliminarRoles(String idRol) {
            execute("delete from Roles where idRol = '"+idRol+"'");
        }

        public synchronized Roles getItemForId(String idRol){
            ArrayList<Roles> lst = executeQuery("select * from Roles where idRol = '"+idRol+"'");
            if (lst.size()>0){
                return lst.get(0);
            }else{
                return new Roles();
            }
        }

}